package com.wondersgroup.esclient.feign;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import feign.Feign;

public class FeignUitls {
	/**
	 * Get请求
	 * 
	 * @param httpUrl
	 * @param parMap
	 *            其中包含headerParams和urlParams两种参数
	 * @return
	 * @throws Exception
	 */
	public static String httpGet(String httpUrl, Map<String, Object> parMap) throws Exception {
		URI uri = null;
		// head param
		Map<String, Object> headerMap = (null == parMap.get("headerParams")
				|| "".equals(parMap.get("headerParams").toString().trim())) ? null
						: (Map<String, Object>) parMap.get("headerParams");

		// url param
		Map<String, String> urlMap = (null == parMap.get("urlParams")
				|| "".equals(parMap.get("urlParams").toString().trim())) ? null
						: (Map<String, String>) parMap.get("urlParams");

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		if (urlMap != null && urlMap.size() > 0) {
			for (String key : urlMap.keySet()) {
				nameValuePairs.add(new BasicNameValuePair(key, urlMap.get(key)));
			}
			String str = EntityUtils.toString(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			if (httpUrl.indexOf("?") > 0) {
				uri = new URI(httpUrl + "&" + str);
			} else {
				uri = new URI(httpUrl + "?" + str);
			}
		} else {
			uri = new URI(httpUrl);
		}

		// call
		FeignInterface reignInterface = Feign.builder().target(FeignInterface.class, uri.toString());
		String response = "";
		if (headerMap != null) {
			response = reignInterface.httpGet(headerMap);
		} else {
			response = reignInterface.httpGet();
		}
		return response;
	}

	/**
	 * Get请求
	 * @param httpUrl
	 * @return
	 * @throws Exception
	 */
	public static String httpGet(String httpUrl) throws Exception {
		URI uri = new URI(httpUrl);
		// call
		FeignInterface reignInterface = Feign.builder().target(FeignInterface.class, uri.toString());
		String response = reignInterface.httpGet();
		return response;
	}

	/**
	 * Post请求
	 * @param httpUrl
	 * @param bodyParams
	 * @return
	 * @throws Exception
	 */
	public static String httpPost(String httpUrl, String bodyParams) throws Exception {
		URI uri = new URI(httpUrl);
		// call
		FeignInterface reignInterface = Feign.builder().target(FeignInterface.class, uri.toString());
		return reignInterface.httpPost(bodyParams);

	}
	
	/**
	 * Post请求
	 * @param httpUrl
	 * @param parMap
	 * @return
	 * @throws Exception
	 */
	public static String httpPost(String httpUrl, Map<String, Object> parMap) throws Exception {
		URI uri = null;
		// head params
		Map<String, Object> headerMap = (null == parMap.get("headerParams")
				|| "".equals(parMap.get("headerParams").toString().trim())) ? null
						: (Map<String, Object>) parMap.get("headerParams");
		uri = new URI(httpUrl);
		// body params
		String bodyStr = (null == parMap.get("bodyParams") || "".equals(parMap.get("bodyParams").toString().trim()))
				? null
						: parMap.get("bodyParams").toString();
		// call
		FeignInterface reignInterface = Feign.builder().target(FeignInterface.class, uri.toString());
		String response = "";
		if (headerMap != null) {
			response = reignInterface.httpPost(headerMap, bodyStr);
		} 
		return response;
	}
	
	/**
	 * Put请求
	 * @param httpUrl
	 * @param bodyParams
	 * @return
	 * @throws Exception
	 */
	public static String httpPut(String httpUrl, String bodyParams) throws Exception {
		URI uri = null;
		uri = new URI(httpUrl);
		// call
		FeignInterface reignInterface = Feign.builder().target(FeignInterface.class, uri.toString());
		return reignInterface.httpPut(bodyParams);
	}
	
	/**
	 * Put请求
	 * @param httpUrl
	 * @param parMap
	 * @return
	 * @throws Exception
	 */
	public static String httpPut(String httpUrl, Map<String, Object> headerParams, String bodyParams) throws Exception {
		URI uri = new URI(httpUrl);
		// call
		FeignInterface reignInterface = Feign.builder().target(FeignInterface.class, uri.toString());
		return reignInterface.httpPut(headerParams, bodyParams);
	}
}
